package com.auerdi.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDialogFragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.auerdi.AnimationUtils.AnimationUtils;
import com.auerdi.GlideApp;
import com.auerdi.R;
import com.auerdi.dialog.view.SearchView;
import com.example.egghttp.EggLog.L;
import com.example.egghttp.RetrofitClient;
import com.example.egghttp.retrofit.AdService;
import com.example.egghttp.retrofit.beans.BOb;
import com.example.egghttp.retrofit.beans.banner.SearchInfor;
import com.example.egghttp.retrofit.beans.goods.Product;
import com.squareup.picasso.Picasso;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by highsixty on 2018/3/15.
 * mail  gaolulin@sunmi.com
 */

public class SearchGoodsDialog extends AppCompatDialogFragment {


    Unbinder unbinder;
    @BindView(R.id.recy)
    RecyclerView recy;
    @BindView(R.id.container)
    LinearLayout container;
    @BindView(R.id.search)
    SearchView search;
    //    private static Product mProduct;
    private List<Product> products = new ArrayList<>();

    public SearchGoodsDialog() {
        super();
    }

    public static SearchGoodsDialog newInstance(SelectGood selectGood) {

        SearchGoodsDialog fragment = new SearchGoodsDialog();
        fragment.setListener(selectGood);
        Bundle args = new Bundle();
//        args.putString("buyType", buyType);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();

        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);


        Window window = getDialog().getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
//        windowParams.dimAmount = 0.0f;

        window.setAttributes(windowParams);
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_category_segood, container, false);
        unbinder = ButterKnife.bind(this, view);
        AnimationUtils.slideToUp(view);

        search.setTextWatcher(new SearchView.SearchWatcher() {
            @Override
            public void afterTextChanged(String input) {
                L.d("hcia", "input:" + input);
                List<Product> myList = Observable.from(products)
                        .filter(new Func1<Product, Boolean>() {
                            @Override
                            public Boolean call(Product product) {
                                return product.getTitle().contains(input);
                            }
                        })
                        .toList().toBlocking()
                        .first();


                recy.setAdapter(new CommonAdapter<Product>(getActivity(), R.layout.item_searchpr, myList) {
                    @Override
                    protected void convert(ViewHolder viewHolder, final Product item, int position) {
                        ImageView gPic = viewHolder.getView(R.id.img);
                        viewHolder.setText(R.id.title, "" + item.getTitle());
                        viewHolder.setText(R.id.subtitle, "" + item.getShareTitle());
                        Picasso.get().load(item.getLunboImg()).error(R.mipmap.logo).placeholder(R.mipmap.logo).into(gPic);//

                    }
                });
            }

            @Override
            public void emptyText() {
                L.d("hcia", "emptyText:");
                recy.setAdapter(new CommonAdapter<Product>(getActivity(), R.layout.item_searchpr, products) {
                    @Override
                    protected void convert(ViewHolder viewHolder, final Product item, int position) {
                        ImageView gPic = viewHolder.getView(R.id.img);
                        viewHolder.setText(R.id.title, "" + item.getTitle());
                        viewHolder.setText(R.id.subtitle, "" + item.getShareTitle());
//                        Picasso.get().load(item.getLunboImg()).error(R.mipmap.logo).placeholder(R.mipmap.logo).into(gPic);//

                        GlideApp.with(viewHolder.getConvertView().getContext())
                                .load(item.getLunboImg())
                                .placeholder(R.mipmap.logo)
                                .error(R.mipmap.pay_err)
                                .centerCrop()
                                .into(gPic);

                    }
                });

            }
        });

        recy.setLayoutManager(new GridLayoutManager(getContext(), 1));


        RetrofitClient.getPDAClient().create(AdService.class).experienceSearchPro()
                .subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.io())

                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BOb<SearchInfor>>() {
                    @Override
                    public void onCompleted() {
                        L.d("hcia", "e:");

                    }

                    @Override
                    public void onError(Throwable e) {
                        L.d("hcia", "e:" + e);
                        L.d("hcia", "e:" + e);

                    }

                    @Override
                    public void onNext(BOb<SearchInfor> searchInforBOb) {
                        List<Product> lists = searchInforBOb.getBody().getLists();


                        products.clear();
                        products.addAll(lists);
                        recy.setAdapter(new CommonAdapter<Product>(getActivity(), R.layout.item_searchpr, products) {
                            @Override
                            protected void convert(ViewHolder viewHolder, final Product item, int position) {

                                ImageView gPic = viewHolder.getView(R.id.img);
                                viewHolder.setText(R.id.title, "" + item.getTitle());
                                viewHolder.setText(R.id.subtitle, "" + item.getShareTitle());
                                GlideApp.with(viewHolder.getConvertView().getContext())
                                        .load(item.getLunboImg())
                                        .placeholder(R.mipmap.logo)
                                        .error(R.mipmap.pay_err)
                                        .centerCrop()
                                        .into(gPic);
                                viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {

                                        listener.Select(item);
                                        dismiss();
                                    }
                                });

                            }
                        });

                    }
                });


        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


    }

    @NonNull
    private RecyclerView.ItemDecoration getDecor() {
        return new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
//                outRect.top = AnimationUtils.dp2px(recy.getContext(), 10);
//                outRect.left = AnimationUtils.dp2px(recy.getContext(), 10);
//                outRect.right = AnimationUtils.dp2px(recy.getContext(), 10);
//                outRect.bottom = AnimationUtils.dp2px(recy.getContext(), 10);
            }
        };
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroyView() {


        super.onDestroyView();
        unbinder.unbind();
    }

    public void setListener(SelectGood listener) {
        this.listener = listener;
    }

    private SearchGoodsDialog.SelectGood listener = null;


    public interface SelectGood {
        void Select(Product num);
    }
}
